/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jsf.bean.client;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ValueChangeEvent;
import model.client.City;

/**
 *
 * @author Administrator
 */
@ManagedBean
@RequestScoped
public class CityBean_c implements Serializable {
    private Connection conn;
    private final String TABLE_CITIES = "Cities";
    private final String CITY_ID = "City_Id";
    private final String COUNTRY_ID = "Country_Id";
    private final String CITY_NAME = "City_Name";  
    private int selectedCityId;

    public int getSelectedCityId() throws SQLException {
        if(selectedCityId == 0)
            selectedCityId = listCities().get(0).getCityId();
        return selectedCityId;
    }

    public void setSelectedCityId(int selectedCityId) {
        this.selectedCityId = selectedCityId;
    }
    
    /**
     * Creates a new instance of CityBean_c
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public CityBean_c() throws SQLException, ClassNotFoundException {
        conn = JDBCUtil.MyConn.getConnection();
    }
    
    public List<City> listCities() throws SQLException {
        PreparedStatement pst = conn.prepareStatement("SELECT * FROM " + TABLE_CITIES);
        ResultSet rs = pst.executeQuery();
        
        List<City> list = new ArrayList<City>();
        while(rs.next()){
            list.add(new City(rs.getInt(CITY_ID), rs.getInt(COUNTRY_ID), rs.getString(CITY_NAME)));
        }
        
        return list;
    }
    
    public void valueChangeMethod(ValueChangeEvent e){
		String str = "";
	}
}
